package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.res.OrderInfoRes;
import com.easy.dfcapi.res.OrderRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.res.ResponseUser;
import com.easy.dfcapi.service.ApiOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
 * 订单相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/order")
public class ApiOrderController extends Api20Controller {


    @Autowired
    private ApiOrderService orderService;


    /**
     * 买家订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderList", method = RequestMethod.POST)
    public Result BuyerOrderList(Response<OrderInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }



    /**
     * 添加订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderAdd", method = RequestMethod.POST)
    public Result OrderAdd(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 取消订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderCancel", method = RequestMethod.POST)
    public Result BuyerOrderCancel(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderCancel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 删除订单进回收站
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderDel", method = RequestMethod.POST)
    public Result BuyerOrderDel(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 从回收站恢复订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderRec", method = RequestMethod.POST)
    public Result BuyerOrderRec(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderRec(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 真正删除订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderDelete", method = RequestMethod.POST)
    public Result BuyerOrderDelete(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderDelete(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 订单详情
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderDetail", method = RequestMethod.POST)
    public Result BuyerOrderDetail(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 订单退款发起
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderRefunds", method = RequestMethod.POST)
    public Result BuyerOrderRefunds(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderRefunds(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 订单同意退货后发起退货填入运单号
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderReturn", method = RequestMethod.POST)
    public Result BuyerOrderReturn(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderReturn(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 订单退款详情
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderRefundsDetail", method = RequestMethod.POST)
    public Result BuyerOrderRefundsDetail(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderRefundsDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 订单确认收货
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BuyerOrderReceipt", method = RequestMethod.POST)
    public Result BuyerOrderReceipt(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.BuyerOrderReceipt(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    //==================================================================================================================
    //                      以上为买家订单处理
    //==================================================================================================================
    //                      以下为商家订单处理
    //==================================================================================================================
    /**
     * 商家订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderList", method = RequestMethod.POST)
    public Result SellerOrderList(Response<OrderInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家订单发货
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderDeliver", method = RequestMethod.POST)
    public Result SellerOrderDeliver(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderDeliver(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家缺货取消
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderOut", method = RequestMethod.POST)
    public Result SellerOrderOut(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderOut(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家对退货申请进行审核
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderRefundsAudit", method = RequestMethod.POST)
    public Result SellerOrderRefundsAudit(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderRefundsAudit(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 订单退款详情
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderRefundsDetail", method = RequestMethod.POST)
    public Result SellerOrderRefundsDetail(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderRefundsDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家对退货申请进行审核
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderReturnAudit", method = RequestMethod.POST)
    public Result SellerOrderReturnAudit(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderReturnAudit(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家取消订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderCancel", method = RequestMethod.POST)
    public Result SellerOrderCancel(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderCancel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 商家删除订单进回收站
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderDel", method = RequestMethod.POST)
    public Result SellerOrderDel(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 商家从回收站恢复订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderRec", method = RequestMethod.POST)
    public Result SellerOrderRec(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderRec(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商家真正删除订单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderDelete", method = RequestMethod.POST)
    public Result SellerOrderDelete(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderDelete(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 商家订单详情
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SellerOrderDetail", method = RequestMethod.POST)
    public Result SellerOrderDetail(Response<OrderRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return orderService.SellerOrderDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
